package com.salatigacode.customerdatabase;

import java.util.ArrayList;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import spring.context.SpringContext;

import com.salatigacode.customerdatabase.dao.ConnectionDB;
import com.salatigacode.customerdatabase.dao.CustomerDAO;
import com.salatigacode.customerdatabase.dao.CustomerDAOSpring;
import com.salatigacode.customerdatabase.entity.Customer;

public class CustomerView extends ViewPart implements ISelectionListener {

	public static final String ID = "com.salatigacode.customerdatabase.CustomerView";
	private Text txtId;
	private Text txtFName;
	private Text txtLName;
	private Text txtPhone;
	private Text txtEmail;
	private java.util.List<Customer> customers = new ArrayList<Customer>();
	private CustomerDAOSpring dao;
	private List listCustomer;
	private int selectedIndex = -1;

	public CustomerView() {
		dao = (CustomerDAOSpring)SpringContext.getContext().getBean("dao");
		loadCustomers();
	}

	private void loadCustomers() {
		customers = dao.getAll();
	}

	private void loadList() {
		listCustomer.removeAll();
		for (Customer customer : customers) {
			listCustomer.add(customer.getFirstName());
		}
	}

	private void bindForm() {
		Customer currentCust = customers.get(selectedIndex);
		txtId.setText(currentCust.getId() + "");
		txtFName.setText(currentCust.getFirstName());
		txtLName.setText(currentCust.getLastName());
		txtPhone.setText(currentCust.getPhone());
		txtEmail.setText(currentCust.getEmail());
	}

	class MyButtonEvent extends MouseAdapter {
		private int navigasi;

		public MyButtonEvent(int navigasi) {
			this.navigasi = navigasi;
		}

		@Override
		public void mouseUp(MouseEvent e) {
			switch (navigasi) {
			case 0:
				selectedIndex = 0;
				listCustomer.setSelection(selectedIndex);
				bindForm();
				break;
			case 1:
				if (selectedIndex > 0)
					--selectedIndex;
				listCustomer.setSelection(selectedIndex);
				bindForm();
				break;
			case 2:
				if (selectedIndex < customers.size() - 1)
					++selectedIndex;
				listCustomer.setSelection(selectedIndex);
				bindForm();
				break;
			case 3:
				selectedIndex = customers.size() - 1;
				listCustomer.setSelection(selectedIndex);
				bindForm();
				break;
			default:
				selectedIndex = 0;
				listCustomer.setSelection(selectedIndex);
				bindForm();
				break;
			}
		}
	}

	@Override
	public void createPartControl(final Composite parent) {
		
		getSite().getPage().addSelectionListener(this);
		
		parent.setLayout(new FormLayout());

		listCustomer = new List(parent, SWT.BORDER);
		listCustomer.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				selectedIndex = listCustomer.getSelectionIndex();
				bindForm();
			}
		});

		FormData fd_listCustomer = new FormData();
		fd_listCustomer.top = new FormAttachment(0, 10);
		fd_listCustomer.bottom = new FormAttachment(100, -69);
		fd_listCustomer.left = new FormAttachment(0, 10);
		listCustomer.setLayoutData(fd_listCustomer);

		Group grpDetailCustomer = new Group(parent, SWT.NONE);
		fd_listCustomer.right = new FormAttachment(grpDetailCustomer, -6);
		FormData fd_grpDetailCustomer = new FormData();
		fd_grpDetailCustomer.right = new FormAttachment(100, -10);
		fd_grpDetailCustomer.left = new FormAttachment(0, 203);
		fd_grpDetailCustomer.top = new FormAttachment(0, 10);
		fd_grpDetailCustomer.bottom = new FormAttachment(0, 163);
		grpDetailCustomer.setLayoutData(fd_grpDetailCustomer);
		grpDetailCustomer.setText("Detail Customer");
		grpDetailCustomer.setLayout(new GridLayout(2, false));

		Label lblId = new Label(grpDetailCustomer, SWT.NONE);
		lblId.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblId.setText("ID :");

		txtId = new Text(grpDetailCustomer, SWT.BORDER);
		txtId.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label lblFirstName = new Label(grpDetailCustomer, SWT.NONE);
		lblFirstName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblFirstName.setText("First Name :");

		txtFName = new Text(grpDetailCustomer, SWT.BORDER);
		txtFName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblLastName = new Label(grpDetailCustomer, SWT.NONE);
		lblLastName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblLastName.setText("Last Name :");

		txtLName = new Text(grpDetailCustomer, SWT.BORDER);
		txtLName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblPhone = new Label(grpDetailCustomer, SWT.NONE);
		lblPhone.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblPhone.setText("Phone :");

		txtPhone = new Text(grpDetailCustomer, SWT.BORDER);
		txtPhone.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblEmail = new Label(grpDetailCustomer, SWT.NONE);
		lblEmail.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblEmail.setText("Email :");

		txtEmail = new Text(grpDetailCustomer, SWT.BORDER);
		txtEmail.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Group group_1 = new Group(parent, SWT.NONE);
		group_1.setLayout(new GridLayout(9, false));
		FormData fd_group_1 = new FormData();
		fd_group_1.top = new FormAttachment(grpDetailCustomer, 6);
		fd_group_1.right = new FormAttachment(grpDetailCustomer, 0, SWT.RIGHT);
		fd_group_1.left = new FormAttachment(listCustomer, 6);
		group_1.setLayoutData(fd_group_1);

		final Button btnInsert = new Button(group_1, SWT.NONE);
		btnInsert.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if (btnInsert.getText().equalsIgnoreCase("INSERT")) {
					btnInsert.setText("Save");
					txtId.setText("");
					txtId.setEnabled(false);
					txtFName.setText("");
					txtLName.setText("");
					txtPhone.setText("");
					txtEmail.setText("");
					txtFName.setFocus();
				} else {
					btnInsert.setText("Insert");
					Customer cust = new Customer();
					cust.setFirstName(txtFName.getText());
					cust.setLastName(txtLName.getText());
					cust.setPhone(txtPhone.getText());
					cust.setEmail(txtEmail.getText());
					dao.insert(cust);
					loadCustomers();
					loadList();
					selectedIndex++;
					bindForm();
				}
			}
		});
		btnInsert.setText("Insert");

		Button btnDelete = new Button(group_1, SWT.NONE);
		btnDelete.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				MessageBox dialog = new MessageBox(parent.getShell(),
						SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
				dialog.setText("Confirmation");
				dialog.setMessage("Do you really want to delete this?");
				if (dialog.open() == SWT.OK) {
					// hapus
					dao.delete(customers.get(selectedIndex).getId());
					loadCustomers();
					if (customers.size() > 0) {
						loadList();
						selectedIndex = 0;
						listCustomer.setSelection(selectedIndex);
						bindForm();
					} else {
						selectedIndex = -1;
						listCustomer.removeAll();
						txtId.setText("");
						txtFName.setText("");
						txtLName.setText("");
						txtPhone.setText("");
						txtEmail.setText("");
					}
				}
			}
		});
		btnDelete.setText("Delete");
		new Label(group_1, SWT.NONE);
		new Label(group_1, SWT.NONE);
		new Label(group_1, SWT.NONE);
		new Label(group_1, SWT.NONE);
		new Label(group_1, SWT.NONE);
		new Label(group_1, SWT.NONE);

		Group group = new Group(group_1, SWT.NONE);
		GridData gd_group = new GridData(SWT.CENTER, SWT.CENTER, false, false,
				1, 1);
		gd_group.heightHint = 39;
		group.setLayoutData(gd_group);
		group.setLayout(new GridLayout(4, false));

		Button btnFirst = new Button(group, SWT.NONE);
		btnFirst.addMouseListener(new MyButtonEvent(0));
		btnFirst.setText("|<");

		Button btnPrev = new Button(group, SWT.NONE);
		btnPrev.setText("<<");
		btnPrev.addMouseListener(new MyButtonEvent(1));

		Button btnNext = new Button(group, SWT.NONE);
		btnNext.setText(">>");
		btnNext.addMouseListener(new MyButtonEvent(2));

		Button btnLast = new Button(group, SWT.NONE);
		btnLast.setText(">|");
		btnLast.addMouseListener(new MyButtonEvent(3));

		if (customers.size() > 0) {
			selectedIndex = 0;
			loadList();
			listCustomer.setSelection(selectedIndex);
			bindForm();
		}

		
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {		
			int customerId = Integer.valueOf(((StructuredSelection) selection)
					.getFirstElement().toString());
			updateSelection(customerId);			
	}
	
	public void updateSelection(int Id){
		selectedIndex = 0;
		for (Customer seletedCust : customers) {
			if(seletedCust.getId()==Id){
				break;
			}
			++selectedIndex;
		}
		bindForm();
		listCustomer.setSelection(selectedIndex);
	}
}
